package com.monster.dict.domain.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.monster.dict.domain.common.DictExceptionEnum;
import com.monster.dict.domain.convert.DictConvert;
import com.monster.dict.domain.dto.DictInsertDto;
import com.monster.dict.domain.dto.DictUpdateDto;
import com.monster.dict.domain.dto.QueryDictListDto;
import com.monster.dict.domain.service.SysDictItemService;
import com.monster.dict.domain.service.SysDictService;
import com.monster.dict.domain.vo.QueryDictListVo;
import com.monster.dict.infrastructure.dao.SysDictDao;
import com.monster.dict.infrastructure.dao.SysDictItemDao;
import com.monster.dict.infrastructure.entity.SysDict;
import com.monster.exception.impl.MonsterNoTackException;
import com.monster.starter.core.common.entity.BaseEntityPlusSupper;
import com.monster.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * 字典主表service层
 *
 * @author kuang
 */
@Service
public class SysDictServiceImpl implements SysDictService {
    /**
     * 转换器
     */
    @Autowired
    private DictConvert dictConvert;
    /**
     * 字典类型的dao层
     */
    @Autowired
    private SysDictDao dictDao;
    /**
     * 字典项的service层
     */
    @Autowired
    private SysDictItemService itemService;

    @Override
    public IPage<QueryDictListVo> list(Page page, QueryDictListDto dto) {
        LambdaQueryWrapper<SysDict> wrapper = new LambdaQueryWrapper();
        // 有条件的时候右边模糊查询
        wrapper.likeRight(StringUtils.isNotEmpty(dto.getCode()), SysDict::getCode, dto.getCode());
        wrapper.likeRight(StringUtils.isNotEmpty(dto.getName()), SysDict::getName, dto.getName());
        // 全等查询
        wrapper.eq(Objects.nonNull(dto.getLockState()), SysDict::getLockState, dto.getLockState());
        wrapper.eq(Objects.nonNull(dto.getType()), SysDict::getType, dto.getType());
        wrapper.eq(Objects.nonNull(dto.getState()), SysDict::getState, dto.getState());

        return dictConvert.toDictListPageVo(dictDao.page(page, wrapper));
    }

    /**
     * 心中字典类型数据
     *
     * @param insertDto 字典数据
     * @return boolean
     */
    @Override
    public boolean insert(DictInsertDto insertDto) {
        // 唯一性检查
        if (dictDao.getOne(new LambdaQueryWrapper<SysDict>().eq(SysDict::getCode, insertDto.getCode())) != null){
            throw new MonsterNoTackException(DictExceptionEnum.UNIQUE);
        }
        return dictDao.save(dictConvert.toSysDict(insertDto));
    }

    /**
     * 修改字典类型数据
     * @param updateDto 修改的数据
     * @return boolean
     */
    @Override
    public boolean update(DictUpdateDto updateDto) {
        return dictDao.updateById(dictConvert.toSysDict(updateDto));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(String id) {
        // 查询字典编码
        SysDict dict = dictDao.getById(id);
        itemService.deleteByDictCode(dict.getCode());
        return dictDao.delById(id);
    }
}
